package blackitheima1.ClassTest;

import blackitheima1.Domain.Student;

import java.lang.reflect.Field;

//反射-获取Field对象
public class MyClass3 {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException {
        //创建Class对象
        Class aClass = Class.forName("blackitheima1.Domain.Student");

        //获取成员变量
        Field name = aClass.getDeclaredField("name");

        //暴力反射
        name.setAccessible(true);

        //创建对象
        Student stu = (Student) aClass.newInstance();
        //成员变量的赋值与获取值
        name.set(stu,"张三");
        Object o = name.get(stu);
        System.out.println(o);

    }
}
